package com.atsmart.app.sql;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.atsmart.app.model.NumberResponseInfo;

public class NumberSqlHelper {

	private SQLiteDatabase db = null;

	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}

	public synchronized void addNumberInfo(NumberResponseInfo at) {
		if (at == null) {
			return;
		}
		String strSql = "insert or replace into number_info (type,number,lastTime) values(?,?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			stm.bindLong(1, at.getType());
			stm.bindLong(2, at.getNumber());
			stm.bindLong(3, at.getLastTime());
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			// will not happen
		}
	}

	public List<NumberResponseInfo> getNumberInfo() {
		String sql = " select _id,type,number,lastTime from number_info";
		Cursor cursor = db.rawQuery(sql, null);
		NumberResponseInfo historie = null;
		List<NumberResponseInfo> nums = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				nums = new ArrayList<NumberResponseInfo>();
				for (int i = 0; i < cursor.getCount(); i++) {
					historie = new NumberResponseInfo();
					historie.setType(cursor.getInt(1));
					historie.setNumber(cursor.getInt(2));
					historie.setLastTime(cursor.getLong(3));
					nums.add(historie);
					cursor.moveToNext();
				}
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return nums;
	}

	public NumberResponseInfo getNumberByType(int type) {
		String sql = " select _id,type,number,lastTime from number_info where type="
				+ type;
		Cursor cursor = db.rawQuery(sql, null);
		NumberResponseInfo historie = null;
		try {
			if (cursor != null && cursor.moveToFirst()) {
				historie = new NumberResponseInfo();
				historie.setType(cursor.getInt(1));
				historie.setNumber(cursor.getInt(2));
				historie.setLastTime(cursor.getLong(3));
				historie.setLastTime(cursor.getLong(3));
			}
		} catch (Exception e) {
			return null;
		} finally {
			cursor.close();
		}
		return historie;
	}

	public void deleteAttribute() {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from number_info");
	}
	public void deleteAttributeByType(Integer type) {
		// db.delete("position_attribute", null, null);
		db.execSQL("delete from number_info where type="+type);
	}
	 public void updateAttribute(Integer num,Integer type) {
	 ContentValues cv = new ContentValues();
	 cv.put("number", num);
	 db.update("number_info", cv, "type="+type, null);
	 }
	 public void updateNumber(Integer num,long time,Integer type) {
		 ContentValues cv = new ContentValues();
		 cv.put("number", num);
		 cv.put("lastTime", time);
		 db.update("number_info", cv, "type="+type, null);
		 }
}
